package com.mc.moring.information.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.google.gson.Gson;
import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.BaseFragment;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.bean.AppKycPersonalInformacionConfig;
import com.mc.moring.bean.SingleSelectBean;
import com.mc.moring.bean.ZiLiPageUserDataBean;
import com.mc.moring.information.CapitalInforActivity;
import com.mc.moring.information.modle.KycModle;
import com.mc.moring.util.GsonUtils;
import com.mc.moring.util.PointMgr;
import com.mc.moring.util.T;
import com.mc.moring.util.ToastUtils;
import com.mc.moring.widge.SingleSelectDialogView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class LaboralInformationFragment extends BaseFragment {

    CapitalInforActivity activity;
    NestedScrollView parent;
    TextView tv_sub_to;
    LinearLayout ll_companyname;
    EditText et_companyname;
    TextView tv_companyname;
    TextView tv_companyname_err;
    View companyname_line;
    LinearLayout ll_payrollcycle;
    TextView tv_payrollcycle_tip;
    View payrollcycle_line;
    FrameLayout fl_payrollcycle;
    TextView tv_payrollcycle;
    TextView et_payrollcycle;
    LinearLayout ll_current_profession;
    FrameLayout fl_current_profession;
    TextView tv_current_profession;
    TextView tv_current_profession_tip;
    View profession_line;
    TextView et_current_profession;
    LinearLayout ll_income;
    FrameLayout fl_income;
    TextView tv_income;
    TextView et_income;
    TextView tv_income_tip;
    View income_line;
    LinearLayout ll_payday_1;
    FrameLayout fl_payday_1;
    TextView tv_payday_1;
    TextView et_payday_1;
    TextView tv_payday_1_tip;
    View payday_1_line;

    LinearLayout ll_payday_2;
    FrameLayout fl_payday_2;
    TextView tv_payday_2;
    TextView et_payday_2;
    TextView tv_payday_2_tip;
    View payday_2_line;

    String companyName, workStatus, income, payrollCycle, payDay;
    public KycModle kycModle = null;

    private CapitalInforActivity fatherActivity;


    View view = null;

    private AppKycPersonalInformacionConfig.WorkInfoConfigBean workInfoConfigBean;

    public interface ActivityListener{
        public void sendValue(String value);
    }

    private ActivityListener activityListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        fatherActivity = (CapitalInforActivity) activity;
        activityListener = (ActivityListener) getActivity();
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtils.logE("second page");
        PointMgr.INSTANCE.sendEvent("cl_shuru_name_13_click");
        Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_13_click");
        view = inflater.inflate(R.layout.fragment_laboral_information, container, false);
        kycModle = KycModle.Companion.getInstant();
        initView();
        initData();
        return view;
    }

    private void close() {
        if (getActivity().getCurrentFocus() == null) {
            return;
        }
        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public LaboralInformationFragment(Activity activity) {
        this.activity = (CapitalInforActivity) activity;
    }

    @Override
    public void initView() {
        parent = view.findViewById(R.id.parent);
        tv_sub_to = view.findViewById(R.id.tv_sub_to);
        ll_companyname = view.findViewById(R.id.ll_companyname);
        tv_companyname_err = view.findViewById(R.id.tv_companyname_err);
        companyname_line = view.findViewById(R.id.companyname_line);
        tv_companyname = view.findViewById(R.id.tv_companyname);
        et_companyname = view.findViewById(R.id.et_companyname);
//        et_companyname.addTextChangedListener(new CommonTextWatcher(et_companyname, tv_companyname_err));

        ll_payrollcycle = view.findViewById(R.id.ll_payrollcycle);
        tv_payrollcycle = view.findViewById(R.id.tv_payrollcycle);
        fl_payrollcycle = view.findViewById(R.id.fl_payrollcycle);
        et_payrollcycle = view.findViewById(R.id.et_payrollcycle);
        tv_payrollcycle_tip = view.findViewById(R.id.tv_payrollcycle_tip);
        payrollcycle_line = view.findViewById(R.id.payrollcycle_line);

        ll_current_profession = view.findViewById(R.id.ll_current_profession);
        tv_current_profession = view.findViewById(R.id.tv_current_profession);
        fl_current_profession = view.findViewById(R.id.fl_current_profession);
        et_current_profession = view.findViewById(R.id.et_current_profession);
        tv_current_profession_tip = view.findViewById(R.id.tv_current_profession_tip);
        profession_line = view.findViewById(R.id.profession_line);
        ll_income = view.findViewById(R.id.ll_income);
        tv_income = view.findViewById(R.id.tv_income);
        fl_income = view.findViewById(R.id.fl_income);
        et_income = view.findViewById(R.id.et_income);
        tv_income_tip = view.findViewById(R.id.tv_income_tip);
        income_line = view.findViewById(R.id.income_line);

        ll_payday_1 = view.findViewById(R.id.ll_payday_1);
        tv_payday_1 = view.findViewById(R.id.tv_payday_1);
        fl_payday_1 = view.findViewById(R.id.fl_payday_1);
        et_payday_1 = view.findViewById(R.id.et_payday_1);
        tv_payday_1_tip = view.findViewById(R.id.tv_payday_1_tip);
        payday_1_line = view.findViewById(R.id.payday_1_line);
        ll_payday_2 = view.findViewById(R.id.ll_payday_2);
        tv_payday_2 = view.findViewById(R.id.tv_payday_2);
        fl_payday_2 = view.findViewById(R.id.fl_payday_2);
        et_payday_2 = view.findViewById(R.id.et_payday_2);
        tv_payday_2_tip = view.findViewById(R.id.tv_payday_2_tip);
        payday_2_line = view.findViewById(R.id.payday_2_line);

        ll_payday_2.setVisibility(View.GONE);

        et_companyname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable editable) {
                tv_companyname_err.setVisibility(View.GONE);
                companyname_line.setBackgroundColor(Color.parseColor("#33000000"));
            }
        });
        et_companyname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    PointMgr.INSTANCE.sendEvent("cl_shuru_name_15_click");
                    Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_15_click");
                }
            }
        });
        noChinese(et_companyname);

        fl_payday_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_shuru_name_19_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_19_click");
                close();
                tv_payday_1_tip.setVisibility(View.GONE);
                payday_1_line.setBackgroundColor(Color.parseColor("#33000000"));
                showDay(getPayDaySingleSelectList("0"), 1,
                        "");
            }
        });
        fl_payday_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_shuru_name_20_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_20_click");
                close();
                tv_payday_2_tip.setVisibility(View.GONE);
                payday_2_line.setBackgroundColor(Color.parseColor("#33000000"));
                if (et_payday_1.getText() != null) {
                    if (TextUtils.isEmpty(et_payday_1.getText().toString())) {
                        TextUtils.isEmpty("Día de pago (primero) is not empty");
                        return;
                    }
                    showDay(getPayDaySingleSelectList(et_payday_1.getText().toString()), 2,
                            "");
                } else {
                    TextUtils.isEmpty("Día de pago (primero) is not empty");
                }

            }
        });
        fl_current_profession.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                PointMgr.INSTANCE.sendEvent("cl_shuru_name_16_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_16_click");
                tv_current_profession_tip.setVisibility(View.GONE);
                profession_line.setBackgroundColor(Color.parseColor("#33000000"));
                showProfession(getProfessionSingleSelectList(), 1,
                        tv_current_profession.getText().toString());// workStatus
            }
        });
        fl_payrollcycle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_shuru_name_17_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_17_click");
                close();
                tv_payrollcycle_tip.setVisibility(View.GONE);
                payrollcycle_line.setBackgroundColor(Color.parseColor("#33000000"));
                showProfession(getPayrollCycleSingleSelectList(), 2,
                        tv_payrollcycle.getText().toString());// payrollCycle
            }
        });
        fl_income.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_shuru_name_18_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_18_click");
                close();
                tv_income_tip.setVisibility(View.GONE);
                income_line.setBackgroundColor(Color.parseColor("#33000000"));
                showProfession(getIncomeSingleSelectList(), 3,
                        tv_income.getText().toString());//income
            }
        });



        tv_sub_to.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_commit_work_info_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_commit_work_info_click");
                hideTvError();
                companyName = et_companyname.getText().toString();
                if (TextUtils.equals(payrollCycle, "0")) {
                    payDay = et_payday_1.getText().toString() + "," + et_payday_2.getText().toString();
                } else {
                    payDay = et_payday_1.getText().toString();
                }

                if (workInfoConfigBean == null) {
                    return;
                }
                if (TextUtils.equals(workInfoConfigBean.getCompanyName(),"1")) {
                    if (TextUtils.isEmpty(companyName) || hasChinese(companyName)
                            || companyName.equals("null") || companyName.length() > 128) {
                        linearLayoutOffsetY(parent, ll_companyname);
                        tv_companyname_err.setVisibility(View.VISIBLE);
                        companyname_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }

                if (TextUtils.equals(workInfoConfigBean.getWorkStatus(),"1")) {
                    if (TextUtils.isEmpty(workStatus) || workStatus.equals("null")) {
                        linearLayoutOffsetY(parent, ll_current_profession);
                        tv_current_profession_tip.setVisibility(View.VISIBLE);
                        profession_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }

                if (TextUtils.equals(workInfoConfigBean.getFrequencyOfSalaryPaid(),"1")) {
                    if (TextUtils.isEmpty(payrollCycle) || payrollCycle.equals("null")) {
                        linearLayoutOffsetY(parent, ll_payrollcycle);
                        tv_payrollcycle_tip.setVisibility(View.VISIBLE);
                        payrollcycle_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }

                if (TextUtils.equals(workInfoConfigBean.getIncome(),"1")) {
                    if (TextUtils.isEmpty(income) || income.equals("null")) {
                        linearLayoutOffsetY(parent, ll_income);
                        tv_income_tip.setVisibility(View.VISIBLE);
                        income_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }


                if (TextUtils.equals(workInfoConfigBean.getPayDay(),"1")) {
                    if (TextUtils.isEmpty(et_payday_1.getText())) {
                        linearLayoutOffsetY(parent, ll_payday_1);
                        tv_payday_1_tip.setVisibility(View.VISIBLE);
                        payday_1_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }


                if (TextUtils.equals(workInfoConfigBean.getPayDay2(),"1")) {
                    if (TextUtils.equals(payrollCycle, "0")) {
                        if (TextUtils.isEmpty(et_payday_2.getText())) {
                            linearLayoutOffsetY(parent, ll_payday_2);
                            tv_payday_2_tip.setVisibility(View.VISIBLE);
                            payday_2_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                            return;
                        }
                    }
                }

                Map<String, String> params = new HashMap<String, String>();
                params.put("companyName", companyName);
                params.put("workStatus", workStatus);
                params.put("income", income);
                params.put("payrollCycle", payrollCycle);
                params.put("payDay", payDay);
                LogUtils.logE("page 2 params>" + new Gson().toJson(params));
                fatherActivity.showLoading();
                kycModle.updateZiLiPageJobInfo(params, new HttpCallback() {
                    @Override
                    public void onFaill(@NonNull String e) {
                        CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                        if (capitalInforActivity != null) {
                            capitalInforActivity.onTabSelected(capitalInforActivity.TAG_INDEX_THREE);
                            capitalInforActivity.setTabIndex(2);
                        }
                        fatherActivity.hideLoading();
                    }

                    @Override
                    public void onSuccess(@NonNull String data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            if (kycModle.getStringToJson(jsonObject, "code").equals("0000")) {
                                CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                                if (capitalInforActivity != null) {
                                    PointMgr.INSTANCE.sendEvent("cl_complete_work_info");
                                    Log.e("PointMgr.INSTANCE.sendEvent", "cl_complete_work_info");
                                    capitalInforActivity.onTabSelected(capitalInforActivity.TAG_INDEX_THREE);
                                    capitalInforActivity.setTabIndex(2);
                                }
                            } else {
                                ToastUtils.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        fatherActivity.hideLoading();
                    }
                });
            }
        });


    }

    private void hideTvError() {
        tv_companyname_err.setVisibility(View.GONE);
        tv_current_profession_tip.setVisibility(View.GONE);
        tv_income_tip.setVisibility(View.GONE);
        tv_payrollcycle_tip.setVisibility(View.GONE);
        tv_payday_1_tip.setVisibility(View.GONE);
        tv_payday_2_tip.setVisibility(View.GONE);
        companyname_line.setBackgroundColor(Color.parseColor("#33000000"));
        profession_line.setBackgroundColor(Color.parseColor("#33000000"));
        payrollcycle_line.setBackgroundColor(Color.parseColor("#33000000"));
        income_line.setBackgroundColor(Color.parseColor("#33000000"));
        payday_1_line.setBackgroundColor(Color.parseColor("#33000000"));
        payday_2_line.setBackgroundColor(Color.parseColor("#33000000"));
    }

    @Override
    public void initData() {
        getZiLiPageUserInfoConfig();
        getZiLiPageUserFullInfo();
    }

    private void getZiLiPageUserFullInfo() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("custId", ZiLiPageUserDataBean.getInstance().getCustomerId());
        fatherActivity.showLoading();
        kycModle.getZiLiPageUserFullInfo(params, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                if (capitalInforActivity != null) {
                    capitalInforActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject(data);
                                LogUtils.logE("history info>" + jsonObject.toString());
                                if (kycModle.isSuccess(jsonObject)) {
                                    JSONObject dataJSONObject = jsonObject.optJSONObject("data");
                                    if (dataJSONObject != null) {
                                        showUserInfo(dataJSONObject.optJSONObject("jobInfo"));
                                    }
                                } else {
                                    T.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                fatherActivity.hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                fatherActivity.hideLoading();
            }
        });
    }

    private void getZiLiPageUserInfoConfig() {
        Map<String, String> params = new HashMap<String, String>();
        fatherActivity.showLoading();
        kycModle.getZiLiPageUserInfoConfig(params, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                if (capitalInforActivity != null) {
                    capitalInforActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject(data);
                                if (kycModle.getStringToJson(jsonObject, "code").equals("0000")) {
                                    AppKycPersonalInformacionConfig applicationUserInfoConfigBean = new AppKycPersonalInformacionConfig();
                                    applicationUserInfoConfigBean.initConfigData(jsonObject.optJSONObject("data"));
                                    workInfoConfigBean = applicationUserInfoConfigBean.getWorkInfoConfig();
                                    if (applicationUserInfoConfigBean.getWorkInfoConfig() != null) {
                                        Log.e("----familyInfoConfigBean", GsonUtils.INSTANCE.getGson().toJson(workInfoConfigBean));
                                        isOhterShowView(workInfoConfigBean.getCompanyName(), ll_companyname, tv_companyname, "Nombre de la empresa");
                                        isOhterShowView(workInfoConfigBean.getWorkStatus(), ll_current_profession, tv_current_profession, "Profesión actual");
                                        isOhterShowView(workInfoConfigBean.getFrequencyOfSalaryPaid(), ll_payrollcycle, tv_payrollcycle, "Período de nómina");
                                        isOhterShowView(workInfoConfigBean.getIncome(), ll_income, tv_income, "Salario mensual");
                                        isOhterShowView(workInfoConfigBean.getPayDay(), ll_payday_1, tv_payday_1, "Día de pago (primero)");
                                        isOhterShowView(workInfoConfigBean.getIncome(), ll_payday_2, tv_payday_2, "Día de pago (segundo)");
                                    }
                                } else {
                                    ToastUtils.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                fatherActivity.hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                fatherActivity.hideLoading();
            }
        });

    }

    private void showUserInfo(JSONObject jsonObject) {
        if (TextUtils.isEmpty(jsonObject.optString("companyName")) || jsonObject.optString("companyName").equals("null")) {
//            et_companyname.setText(null);
        } else {
            et_companyname.setText(jsonObject.optString("companyName"));
        }
        workStatus = jsonObject.optString("workStatus");
        List<SingleSelectBean> professionList = getProfessionSingleSelectList();
        if (!TextUtils.isEmpty(workStatus) && !workStatus.equals("null")) {
            if (workStatus.equals("A")) {
                et_current_profession.setText(professionList.get(0).getText());
            } else if (workStatus.equals("B")) {
                et_current_profession.setText(professionList.get(1).getText());
            } else if (workStatus.equals("C")) {
                et_current_profession.setText(professionList.get(2).getText());
            } else if (workStatus.equals("D")) {
                et_current_profession.setText(professionList.get(3).getText());
            } else if (workStatus.equals("E")) {
                et_current_profession.setText(professionList.get(4).getText());
            } else if (workStatus.equals("F")) {
                et_current_profession.setText(professionList.get(5).getText());
            } else if (workStatus.equals("G")) {
                et_current_profession.setText(professionList.get(6).getText());
            } else if (workStatus.equals("H")) {
                et_current_profession.setText(professionList.get(7).getText());
            } else if (workStatus.equals("I")) {
                et_current_profession.setText(professionList.get(8).getText());
            } else if (workStatus.equals("J")) {
                et_current_profession.setText(professionList.get(9).getText());
            } else if (workStatus.equals("K")) {
                et_current_profession.setText(professionList.get(10).getText());
            } else if (workStatus.equals("L")) {
                et_current_profession.setText(professionList.get(11).getText());
            } else if (workStatus.equals("M")) {
                et_current_profession.setText(professionList.get(12).getText());
            } else if (workStatus.equals("N")) {
                et_current_profession.setText(professionList.get(13).getText());
            } else {
                et_current_profession.setText(professionList.get(14).getText());
            }
        }


        List<SingleSelectBean> payrollCycleSingleSelectList = getPayrollCycleSingleSelectList();

        payrollCycle = jsonObject.optString("payrollCycle");
        ll_payday_2.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(payrollCycle) && !payrollCycle.equals("null")) {
            if (payrollCycle.equals("0")) {
                et_payrollcycle.setText(payrollCycleSingleSelectList.get(0).getText());
                ll_payday_2.setVisibility(View.VISIBLE);
            } else if (payrollCycle.equals("1")) {
                et_payrollcycle.setText(payrollCycleSingleSelectList.get(1).getText());
            } else if (payrollCycle.equals("2")) {
                et_payrollcycle.setText(payrollCycleSingleSelectList.get(2).getText());
            } else {
                et_payrollcycle.setText(payrollCycleSingleSelectList.get(3).getText());
            }
        }

        income = jsonObject.optString("income");
        List<SingleSelectBean> incomeList = getIncomeSingleSelectList();
        if (!TextUtils.isEmpty(income) && !income.equals("null")) {
            if (income.equals("A")) {
                et_income.setText(incomeList.get(0).getText());
            } else if (income.equals("B")) {
                et_income.setText(incomeList.get(1).getText());
            } else if (income.equals("C")) {
                et_income.setText(incomeList.get(2).getText());
            } else if (income.equals("D")) {
                et_income.setText(incomeList.get(3).getText());
            } else if (income.equals("E")) {
                et_income.setText(incomeList.get(4).getText());
            } else {
                et_income.setText(incomeList.get(5).getText());
            }
            for (int i = 0; i < incomeList.size(); i++) {
                if (TextUtils.equals(incomeList.get(i).getCode(), income)) {
                    et_income.setText(incomeList.get(i).getText());
                }
            }
        }


        String payDay = jsonObject.optString("payDay");
        if (!TextUtils.isEmpty(payDay) && !payDay.equals("null")) {
            if (payDay.contains(",")) {
                String[] payDays = payDay.split(",");
                et_payday_1.setText(payDays[0]);
                et_payday_2.setText(payDays[1]);
            } else {
                et_payday_1.setText(payDay);
            }
        }
    }


    private void showProfession(List<SingleSelectBean> dataList, int key, String title) {
        SingleSelectDialogView dialogView = new SingleSelectDialogView(activity, view, title);
        dialogView.setSingleSelectListener(new SingleSelectDialogView.SingleSelectListener() {
            @Override
            public void checked(SingleSelectBean s) {
                if (key == 1) {
                    workStatus = s.getCode();
                    et_current_profession.setText(s.getText());
                } else if (key == 2) {
                    if (s.getText().equals("Quincenalmente")) {
                        payrollCycle = "0";
                    } else if (s.getText().equals("Mensualmente")) {
                        payrollCycle = "1";
                    } else if (s.getText().equals("Semanalmente")) {
                        payrollCycle = "2";
                    } else {
                        payrollCycle = "3";
                    }
                    if (TextUtils.equals(payrollCycle, "0")) {
                        ll_payday_2.setVisibility(View.VISIBLE);
                    } else {
                        ll_payday_2.setVisibility(View.GONE);
                    }
                    et_payrollcycle.setText(s.getText());
                    if (et_payrollcycle.getText().toString().equals("Quincenalmente")) {
                        et_payday_2.setText(null);
                    }
                } else if (key == 3) {
                    if (s.getText().equals("Menos que $600.000")) {
                        income = "A";
                    } else if (s.getText().equals("$601.000- $900.000")) {
                        income = "B";
                    } else if (s.getText().equals("$901.000- $1.200.000")) {
                        income = "C";
                    } else if (s.getText().equals("$1.201.000- $1.500.000")) {
                        income = "D";
                    } else if (s.getText().equals("$1.501.000 -$1.800.000")) {
                        income = "E";
                    } else {
                        income = "F";
                    }
                    et_income.setText(s.getText());
                }
                dialogView.dismiss();
                activityListener.sendValue("beLight");
            }

            @Override
            public void closeDialog(){
                dialogView.dismiss();
                activityListener.sendValue("beLight");
            }
        });
        dialogView.showSingleSelectDialog(activity, dataList);
        activityListener.sendValue("beDark");
    }

    public List<SingleSelectBean> getProfessionSingleSelectList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        LinkedList<String> stringList = new LinkedList<>();
        stringList.add("A&Ama de casa");
        stringList.add("B&Estudiante");
        stringList.add("C&Empleado de la ONCE");
        stringList.add("D&Venta ambulante");
        stringList.add("E&Empleado sector agrario");
        stringList.add("F&Contrato fijo (público)");
        stringList.add("G&Contrato temporal (público)");
        stringList.add("H&Contrato fijo (privado)");
        stringList.add("I&Contrato temporal (privado)");
        stringList.add("J&Autónomo");
        stringList.add("K&Prejubilado");
        stringList.add("L&Jubilado");
        stringList.add("M&Pensionista");
        stringList.add("N&Desempleado con prestación");
        stringList.add("O&Desempleado sin prestación");
        for (int i = 0; i < stringList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            String s = stringList.get(i);
            String[] text = s.split("&");
            if (text.length > 1) {
                singleSelectBean.setText(text[1]);
                singleSelectBean.setCode(text[0]);
            } else {
                singleSelectBean.setText(stringList.get(i));
            }
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);

            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    public List<SingleSelectBean> getPayrollCycleSingleSelectList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        LinkedList<String> stringList = new LinkedList<>();
        stringList.add("Quincenalmente");
        stringList.add("Mensualmente");
        stringList.add("Semanalmente");
        stringList.add("Otros");
        for (int i = 0; i < stringList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            String s = stringList.get(i);
            String[] text;
            if (s.contains("&")) {
                text = s.split("&");
                if (text.length > 1) {
                    singleSelectBean.setText(text[1]);
                    singleSelectBean.setCode(text[0]);
                }
            } else {
                singleSelectBean.setText(s);
            }
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBean.setText(stringList.get(i));
            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    public List<SingleSelectBean> getIncomeSingleSelectList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        LinkedList<String> stringList = new LinkedList<>();

        stringList.add("Menos que $600.000");
        stringList.add("$601.000- $900.000");
        stringList.add("$901.000- $1.200.000");
        stringList.add("$1.201.000- $1.500.000");
        stringList.add("$1.501.000 -$1.800.000");
        stringList.add("Más que $1.800.000");
        for (int i = 0; i < stringList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            String s = stringList.get(i);
            String[] text;
            if (s.contains("&")) {
                text = s.split("&");
                if (text.length > 1) {
                    singleSelectBean.setText(text[1]);
                    singleSelectBean.setCode(text[0]);
                }
            } else {
                singleSelectBean.setText(s);
            }
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBean.setText(stringList.get(i));
            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    public List<SingleSelectBean> getPayDaySingleSelectList(String number) {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();

        for (int i = 1; i <= 31; i++) {
            if (String.valueOf(i).equals(number)) {
                continue;
            }
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            singleSelectBean.setText(String.valueOf(i));
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    private void showDay(List<SingleSelectBean> dataList, int key, String title) {
        SingleSelectDialogView dialogView = new SingleSelectDialogView(activity, view, title);
        dialogView.setSingleSelectListener(new SingleSelectDialogView.SingleSelectListener() {
            @Override
            public void checked(SingleSelectBean s) {
                if (key == 1) {
                    et_payday_1.setText(s.getText());
                    et_payday_2.setText(null);
                } else if (key == 2) {
                    if (TextUtils.isEmpty(et_payday_1.getText().toString())) {
                        TextUtils.isEmpty("Día de pago (primero) is not empty");
                        return;
                    }
                    et_payday_2.setText(s.getText());
                }
                dialogView.dismiss();
                activityListener.sendValue("beLight");
            }
            @Override
            public void closeDialog(){
                dialogView.dismiss();
                activityListener.sendValue("beLight");
            }
        });
        dialogView.showSingleSelectDialog(activity, dataList);
        activityListener.sendValue("beDark");
    }

    private class CommonTextWatcher implements TextWatcher {
        private TextView errText;

        private EditText editText;

        public CommonTextWatcher(EditText editText, TextView errText) {
            this.errText = errText;
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

            requireActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String trimmedText = charSequence.toString().trim();

                    if (!trimmedText.isEmpty()) {
                        clearEditTextError();
                    }

                    if (startsWithSpace(charSequence)) {
                        editText.setText(trimmedText);
                        editText.setSelection(trimmedText.length());
                    } else {
                        clearEditTextError();
                    }

                    if (charSequence.length() > 128) {
                        errText.setVisibility(View.VISIBLE);
                    } else {
                        clearEditTextError();
                    }

                    if (hasChinese(charSequence.toString())) {
                        errText.setVisibility(View.VISIBLE);
                    } else {
                        clearEditTextError();
                    }
                }
            });
        }

        @Override
        public void afterTextChanged(Editable editable) {
            for (int i = 0; i < editable.length(); i++) {
                char c = editable.charAt(i);
                if (hasChinese(String.valueOf(c))) {
                    editable.delete(i, i + 1);
                }
            }
        }

        private void clearEditTextError() {

            errText.setVisibility(View.GONE);
        }

        private boolean startsWithSpace(CharSequence charSequence) {
            return charSequence.length() > 0 && charSequence.charAt(0) == ' ';
        }
    }

    @Override
    public void onPause(){
        super.onPause();
        fatherActivity.hideLoading();
    }

    @Override
    public void onStop() {
        super.onStop();
        PointMgr.INSTANCE.sendEvent("cl_shuru_name_14_click");
        Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_name_14_click");
    }

    private void noChinese(EditText editText){
        editText.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int i2, int i3) {
                for (int i = start; i < end; i++) {
                    if (Character.UnicodeBlock.of(charSequence.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }
}
